package com.example.peibei.ui.activity;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;
import android.widget.TextView;

import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.StatusBarUtil;
import com.example.peibei.R;
import com.example.peibei.base.Constant;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.RefreshTokenPresenter;
import com.gyf.immersionbar.ImmersionBar;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.internal.Constants;

public class SplashActivity extends WDActivity {
    @BindView(R.id.tv_time)
    TextView tv_time;

    private boolean isSeek;

    private Handler handler = new Handler(Looper.getMainLooper()) {
        @SuppressLint("HandlerLeak")
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 2){
                seek();
                return;
            }
            tv_time.setText("跳过" +  msg.obj + "s");
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @OnClick(R.id.tv_time)
    public void seek() {
        if (isSeek){
            return;
        }
        isSeek = true;
        SPUtils  spUtils = new SPUtils(SplashActivity.this, SpConstant.USER_TOKEN);
        String token = spUtils.getString(SpConstant.USER_TOKEN);
        if(TextUtils.isEmpty(token)){
            intent(MainActivity.class);
        }else{
            intent(MainActivity.class);//跳转到主页面
        }
        finish();
    }

    @Override
    protected void initView() {
        ImmersionBar.with(this)
                .fitsSystemWindows(false)
                .fullScreen(true)
                .init();

        SPUtils spUtils = new SPUtils(this,SpConstant.USER_TOKEN);
        RefreshTokenPresenter presenter = new RefreshTokenPresenter(new DataCall<String>() {
            @Override
            public void success(String data, Object... args) {
                spUtils.putString(SpConstant.USER_TOKEN,data);
            }

            @Override
            public void fail(ApiException data, Object... args) {

            }
        });
        presenter.reqeust(spUtils.getString(SpConstant.USER_TOKEN));

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 2; i >= 0; i--) {
                    Message msg = new Message();
                    msg.obj = i;
                    handler.sendMessage(msg);
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (isFinishing()){
                        return;
                    }
                    if (i == 0){
                        Message msg2 = new Message();
                        msg2.what =2;
                        msg2.obj = i;
                        handler.sendMessage(msg2);
                    }
                }
            }
        }).start();
    }

    @Override
    protected void destoryData() {

    }
}
